package com.ujs.edu.medicine.dao;

import com.ujs.edu.medicine.dataobject.TableChatMes;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface TableChatMesMapper {
    @Delete({
        "delete from table_chat_mes",
        "where chatId = #{chatid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer chatid);

    @Insert({
        "insert into table_chat_mes (chatId, fromuserId, ",
        "touserId, prescriptionId)",
        "values (#{chatid,jdbcType=INTEGER}, #{fromuserid,jdbcType=INTEGER}, ",
        "#{touserid,jdbcType=INTEGER}, #{prescriptionid,jdbcType=INTEGER})"
    })
    @Options(useGeneratedKeys = true,keyColumn = "chatId",keyProperty = "chatid")
    int insert(TableChatMes record);

    @Select({
        "select",
        "chatId, fromuserId, touserId, prescriptionId",
        "from table_chat_mes",
        "where chatId = #{chatid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="chatId", property="chatid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="fromuserId", property="fromuserid", jdbcType=JdbcType.INTEGER),
        @Result(column="touserId", property="touserid", jdbcType=JdbcType.INTEGER),
        @Result(column="prescriptionId", property="prescriptionid", jdbcType=JdbcType.INTEGER)
    })
    TableChatMes selectByPrimaryKey(Integer chatid);

    @Select({
        "select",
        "chatId, fromuserId, touserId, prescriptionId",
        "from table_chat_mes"
    })
    @Results({
        @Result(column="chatId", property="chatid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="fromuserId", property="fromuserid", jdbcType=JdbcType.INTEGER),
        @Result(column="touserId", property="touserid", jdbcType=JdbcType.INTEGER),
        @Result(column="prescriptionId", property="prescriptionid", jdbcType=JdbcType.INTEGER)
    })
    List<TableChatMes> selectAll();

    /*通过fromuserID查询chatID*/
    @Select("select * from table_chat_mes where fromuserId=#{fromuserId}")
    List<TableChatMes> selectByFromUserID(@Param("fromuserId")Integer fromuserId);


    /*通过fromuserID或touserID获取chat信息*/
    @Select("select * from table_chat_mes where fromuserId=#{fromuserId} or touserId=#{fromuserId}")
    List<TableChatMes> selectByFromUserIDOrTouserId(@Param("fromuserId")Integer fromuserId);

    /*通过fromuserID或touserID获取chat信息*/
    @Select("select * from table_chat_mes where fromuserId=#{fromuserId} and touserId=#{touserId}")
    TableChatMes selectByFromUserIDAndTouserId(@Param("fromuserId")Integer fromuserId,@Param("touserId")Integer touserId);

    /*查看对方是否有和自己之前联系过*/
    @Select("select * from table_chat_mes where touserId=#{fromuserId}")
    TableChatMes selectToUserId(@Param("fromuserId")Integer fromuserId);

    @Update({
        "update table_chat_mes",
        "set fromuserId = #{fromuserid,jdbcType=INTEGER},",
          "touserId = #{touserid,jdbcType=INTEGER},",
          "prescriptionId = #{prescriptionid,jdbcType=INTEGER}",
        "where chatId = #{chatid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(TableChatMes record);
}